import {Component, OnInit} from '@angular/core';
import {NavController} from 'ionic-angular';


import {UserService} from "../../../providers/user-service";
import {services} from "../../../providers/service";
import {AppPopup} from "../../../util/popup";
import {UserLogin} from "../user-login/user-login";

@Component({
    selector: 'modify-user',
    templateUrl: 'modify-user.html'
})
export class ModifyUser implements OnInit {
    personalDetail = {
        "companyAddressId": "",
        "companyAddressName": "",
        "companyId": "",
        "companyName": "",
        "id": "",
        "mobile": "",
        "name": "",
        "type": "0",
        "companyMemberLimit": "",
        "companyMobile": ""
    };

    errorMsg = '';  //表单校验

    cityData: any[]; //城市数据
    code: string = ''; //城市编码
    area: any;

    constructor(public navCtrl: NavController,
                private popup: AppPopup,
                private userServ: UserService,
                public service: services) {
    }

    ngOnInit() {
        this.userServ.getPersonInfo()
            .then(data => {
                this.personalDetail = data.infoData;
                this.cityData = this.service.cityData;      //获取城市信息
                this.area = this.service.compareCode(this.personalDetail.companyAddressId);
                this.area = this.area.join(' - ');
            })
            .catch(err => {
                console.error("ModifyPwd，获取个人信息失败：", err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.navCtrl.setRoot(UserLogin);
                        })
                } else {
                    this.popup.alert(err.errorMassage);
                }
            });
    }

    /**
     * 城市选择器被改变时触发的事件
     * @param event
     */
    cityChange(event) {
        console.log(event, event['region'].value);
        this.code = event['region'].value;
        this.personalDetail.companyAddressId = '0,' + event['province'].value + ',' + event['city'].value + ',' + event['region'].value;
    }

    //返回
    goBack() {
        this.navCtrl.pop();
    }

    //修改用户信息
    save() {
        let regName = new RegExp('^[(\u4E00-\u9FA5)| (\u0061-\u007a) | (\u0041-\u005a) | (a-zA-Z) ]{2,8}$');
        let regCompanyName = new RegExp('^[(\u4E00-\u9FA5)| (\u0061-\u007a) | (\u0041-\u005a) | (a-zA-Z) ]{1,30}$');
        if (!this.personalDetail.name) {
            this.errorMsg = '姓名不可为空！';
            return;
        } else if (!regName.test(this.personalDetail.name)) {
            this.errorMsg = "姓名控制2-8个字符，只能添加汉字和英文字母";
            return;
        } else if (!this.personalDetail.companyName) {
            this.errorMsg = '公司名不可为空！';
            return;
        } else if (!regCompanyName.test(this.personalDetail.companyName)) {
            this.errorMsg = "公司名控制1-30个字符，只能添加中文和英文字母";
            return;
        }
        else if (!this.personalDetail.companyAddressId) {
            this.errorMsg = '省市区不可为空！';
            return;
        }
        let param = {
            "companyAddressId": this.personalDetail.companyAddressId,
            "companyAddressName": this.personalDetail.companyAddressName,
            "companyName": this.personalDetail.companyName,
            "name": this.personalDetail.name
        }
        console.log(this.personalDetail);
        this.userServ.editPersonalData(param)
            .then(data => {
                console.info("save，用户修改成功：",data)
                    this.navCtrl.pop();
            })
            .catch(err => {
                console.error("save，用户修改失败：",err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.navCtrl.setRoot(UserLogin);
                        })
                } else {
                    this.popup.alert(err.errorMassage);
                }
            });
    }

}
